package com.acegear.horizon.domain.services;

import com.acegear.horizon.domain.models.constraint.ContentType;
import com.acegear.horizon.domain.models.jpa.ContentPart;

import org.jsoup.nodes.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by wangsike on 2017/3/9.
 */
@Service
public class ContentPartService {
    @Autowired
    private PictureService pictureService;

    public void format(List<ContentPart> contentParts) {
        int i = 0;
        for (ContentPart contentPart : contentParts) {
            contentPart.setPos(i++);
        }
        contentParts
                .parallelStream()
                .filter(contentPart -> contentPart.getType() == ContentType.IMAGE || contentPart.getType() == ContentType.COVER)
                .forEach(contentPart -> {
                    contentPart.setContent(contentPart.getContent().replace("//w700", "/w700"));
                    contentPart.setExtra(pictureService.imageInfo(contentPart.getContent()));
                });
        contentParts
                .parallelStream()
                .filter(contentPart -> contentPart.getType() == ContentType.SUMMARY || contentPart.getType() == ContentType.TEXT)
                .filter(contentPart -> !contentPart.getContent().startsWith("<"))
                .forEach(contentPart -> {
                            Element element = new Element("p");
                            element.append(contentPart.getContent().replace("\n", "<br>"));
                            element.attr("style", "font-size:16px;line-height:1.5;");
                            contentPart.setContent(element.toString());
                        }
                );
    }
}
